package myweb.myhttp;


/**
 *  url:Uniform Resource Locator 统一资源定位符
 *
 *
 *
 * */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

/**
 * java.net
 *  RULConnection
 *  .获取资源的连接器
 *  根据URL的openConnection()方法获取URLConnection
 *  connect方法，建立和资源的联系通道
 *  getInputStream方法，获取资源的内容
 * */
public class URlConnectionGetTest {

    public static void main(String[] args) {
        String urlName = "http://www.baidu.com";
        try {
            URL url = new URL(urlName);
            URLConnection urlConnection = url.openConnection();
            urlConnection.connect();
            //打印http的头部信息
            Map<String, List<String>> headerFields = urlConnection.getHeaderFields();
            for (Map.Entry<String, List<String>> entry : headerFields.entrySet()) {
                String key = entry.getKey();
                for (String value : entry.getValue()) {
                    System.out.println(key+"::"+value);
                }
            }
            //输出将要收到的内容属性信息
            System.out.println("-----------------");
            System.out.println("getContentType:"+urlConnection.getContentType());
            System.out.println("getContentLength:"+urlConnection.getContentLength());
            System.out.println("getContentEncoding:"+urlConnection.getContentEncoding());
            System.out.println("getDate"+urlConnection.getDate());
            System.out.println("getExpiration"+urlConnection.getExpiration());
            System.out.println("getLastModified:"+urlConnection.getLastModified());
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            String line = "";//输出内容
            while ((line=bufferedReader.readLine())!=null){
                System.out.println(line);
            }
            bufferedReader.close();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
